package es.ojugames.yincana.bd;

import java.io.InputStream;
import java.util.Scanner;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import es.ojugames.yincana.R;

public class YincanaSQLHelper extends SQLiteOpenHelper {
	static String dbNombre = "yincana.db";
	static int dbVersion = 1;
	private final Context context;

	
	public YincanaSQLHelper(Context context) {
		super(context, dbNombre, null, dbVersion);
		this.context = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		if(db.isReadOnly()) {
			db = getWritableDatabase();
		}
		
		 InputStream schemaStream = context.getResources().openRawResource(R.raw.script_creacion);
		 Scanner schemaScanner = new Scanner(schemaStream, "UTF-8");
		 schemaScanner.useDelimiter("\n");

		 Log.i("YincanaSQLHelper", "Creating database");
		    try {
		      db.beginTransaction();
		      while (schemaScanner.hasNext()) {
		        String statement = schemaScanner.next();
		        Log.d("YincanaSQLHelper", "Creating database: " + statement);
		        db.execSQL(statement);
		      }
		      db.setTransactionSuccessful();
		    } finally {
		      db.endTransaction();
		    }
	}	


	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
				
	}
	
}
